package org.thalien.roleplay.database;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import org.thalien.roleplay.entity.Province;
import org.thalien.roleplay.entity.Region;
import org.thalien.roleplay.entity.Ruler;
import org.thalien.roleplay.logic.LogicException;
import org.thalien.roleplay.logic.ProvinceLogic;
import org.thalien.roleplay.logic.RegionLogic;
import org.thalien.roleplay.logic.RulerLogic;

import com.google.inject.Inject;

public class CreateDatabase {
	
	@Inject
	private RegionLogic regionLogic;
	
	@Inject
	private RulerLogic rulerLogic;
	
	@Inject
	private ProvinceLogic provinceLogic;
	
	private String newLine = "\r\n";

	public void create(PrintWriter pw) throws IOException {
		pw.write("Creating or updating database");
		pw.write(newLine);
		pw.write(newLine);
		
		this.createRegions(pw);
		this.createProvinces(pw);
	}
	
	private Ruler findOrAddRuler(String rulerName) throws LogicException {
		Ruler ruler = this.rulerLogic.findByName(rulerName);
		if (ruler == null) {
			ruler = new Ruler();
			ruler.setName(rulerName);
			ruler = this.rulerLogic.save(ruler);
		}
		return ruler;
	}
	
	private void createRegions(PrintWriter pw) throws IOException {
		pw.write("Creating or updating regions:"+newLine+newLine);
		
		InputStream input = this.getClass().getResourceAsStream("region.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(input));
		String line = br.readLine();
		while (line != null) {
			pw.write(line);
			pw.write(newLine);
			String[] parts = line.split(";");
			if (parts.length > 0) {
				Region region = this.regionLogic.findByName(parts[0]);
				if (region == null) {
					
					pw.write("Region "+parts[0]+" not found. Inserting"+newLine);
					
					try {
						
						region = new Region();
						region.setName(parts[0]);
						
						if (parts.length > 1) {
							Ruler ruler = this.findOrAddRuler(parts[1]);
							region.setRuler(ruler);
						}
						
						regionLogic.save(region);
					} catch (LogicException e) {
						e.printStackTrace(pw);
					}
					
				} else {
					pw.write("Region "+parts[0]+" found. Will not be inserted."+newLine);
				}
			}
			
			pw.write(newLine);
			
			line = br.readLine();
		}	
	}
	
	private void createProvinces(PrintWriter pw) throws IOException {
		pw.write("Creating or updating provinces:"+newLine+newLine);
		
		InputStream input = this.getClass().getResourceAsStream("province.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(input));
		String line = br.readLine();
		
		while (line != null) {

			pw.write(line);
			pw.write(newLine);
			String[] parts = line.split(";");
			if (parts.length >= 4) {
				Province province = this.provinceLogic.findByName(parts[1]);
				
				if (province == null) {
					Region region = this.regionLogic.findByName(parts[0]);
					if (region != null) {
						try {
							province = new Province();
							province.setRegion(region);
							province.setName(parts[1]);
							province.setPopulationLvl(new Integer(parts[2]));
							province.setMaxSourceLvl(new Integer(parts[3]));
							
							if (parts.length > 4) {
								Ruler ruler = this.findOrAddRuler(parts[4]);
								province.setRuler(ruler);
							}
							this.provinceLogic.save(province);
						} catch (NumberFormatException e) {
							e.printStackTrace(pw);
						} catch (LogicException e) {
							e.printStackTrace(pw);
						}
					}
				}
			}
			
			line = br.readLine();
		}
	}
}
